set ns [new Simulator]


#Open the Trace file
set file1 [open ptrace.tr w]
$ns trace-all $file1

#Open the NAM trace file
set file2 [open ptrace.nam w]
$ns namtrace-all $file2

#Define a 'finish' procedure
proc finish {} {
        global ns file1 file2
        $ns flush-trace
        close $file1
        close $file2
        exec nam ptrace.nam &
        exit 0
}

#Next line should be commented out to have the static routing
$ns rtproto DV

#Creat network nodes
#n0 represents the Internet
set n(0) [$ns node]
set n(1) [$ns node]
set n(2) [$ns node]
set n(3) [$ns node]
set n(4) [$ns node]
set n(5) [$ns node]
set n(6) [$ns node]
set n(7) [$ns node]
set n(8) [$ns node]
set n(9) [$ns node]
set n(10) [$ns node]
set n(11) [$ns node]
$n(11) shape "box"
set n(12) [$ns node]
$n(12) shape "box"
set n(13) [$ns node]
$n(13) shape "box"
set n(14) [$ns node]
$n(14) shape "box"
set n(15) [$ns node]
$n(15) shape "box"
set n(16) [$ns node]
$n(16) shape "box"
set n(17) [$ns node]
$n(17) shape "box"
set n(18) [$ns node]
$n(18) shape "box"
set n(19) [$ns node]
$n(19) shape "box"
set n(20) [$ns node]
$n(20) shape "box"
set n(21) [$ns node]
$n(21) shape "box"
set n(22) [$ns node]
$n(22) shape "box"
set n(23) [$ns node]
$n(23) shape "box"
set n(24) [$ns node]
$n(24) shape "box"
set n(25) [$ns node]
$n(25) shape "box"

#Create duplex links between the nodes
$ns duplex-link $n(0) $n(1) 100Mb 10ms DropTail
$ns duplex-link $n(0) $n(2) 100Mb 10ms DropTail
$ns duplex-link $n(0) $n(3) 100Mb 10ms DropTail
$ns duplex-link $n(0) $n(10) 100Mb 10ms DropTail

$ns duplex-link $n(1) $n(4) 100Mb 10ms DropTail
$ns duplex-link $n(1) $n(5) 100Mb 10ms DropTail
$ns duplex-link $n(2) $n(4) 100Mb 10ms DropTail
$ns duplex-link $n(2) $n(5) 100Mb 10ms DropTail

$ns duplex-link $n(4) $n(6) 100Mb 10ms DropTail
$ns duplex-link $n(4) $n(7) 100Mb 10ms DropTail
$ns duplex-link $n(4) $n(8) 100Mb 10ms DropTail
$ns duplex-link $n(4) $n(9) 100Mb 10ms DropTail

$ns duplex-link $n(5) $n(11) 100Mb 10ms DropTail
$ns duplex-link $n(5) $n(12) 100Mb 10ms DropTail
$ns duplex-link $n(5) $n(13) 100Mb 10ms DropTail
$ns duplex-link $n(5) $n(14) 100Mb 10ms DropTail

$ns duplex-link $n(6) $n(15) 100Mb 10ms DropTail
$ns duplex-link $n(6) $n(16) 100Mb 10ms DropTail

$ns duplex-link $n(8) $n(17) 100Mb 10ms DropTail
$ns duplex-link $n(8) $n(18) 100Mb 10ms DropTail

$ns duplex-link $n(9) $n(19) 100Mb 10ms DropTail
$ns duplex-link $n(9) $n(20) 100Mb 10ms DropTail

$ns duplex-link $n(7) $n(21) 100Mb 10ms DropTail
$ns duplex-link $n(7) $n(22) 100Mb 10ms DropTail
$ns duplex-link $n(7) $n(23) 100Mb 10ms DropTail

$ns duplex-link $n(3) $n(24) 100Mb 10ms DropTail

$ns duplex-link $n(10) $n(25) 100Mb 10ms DropTail

#Setup TCP connections for normal traffic
set src(1) [new Agent/UDP]
set src(2) [new Agent/UDP]
set src(3) [new Agent/UDP]
set src(4) [new Agent/UDP]
set src(5) [new Agent/UDP]
set src(6) [new Agent/UDP]
set src(7) [new Agent/UDP]
set src(8) [new Agent/UDP]
set src(9) [new Agent/UDP]
set src(10) [new Agent/UDP]
set src(11) [new Agent/UDP]
set src(12) [new Agent/UDP]
set src(13) [new Agent/UDP]
set src(14) [new Agent/UDP]
set src(15) [new Agent/UDP]

set sink(1) [new Agent/Null]
set sink(2) [new Agent/Null]
set sink(3) [new Agent/Null]
set sink(4) [new Agent/Null]
set sink(5) [new Agent/Null]
set sink(6) [new Agent/Null]
set sink(7) [new Agent/Null]
set sink(8) [new Agent/Null]
set sink(9) [new Agent/Null]
set sink(10) [new Agent/Null]
set sink(11) [new Agent/Null]
set sink(12) [new Agent/Null]
set sink(13) [new Agent/Null]
set sink(14) [new Agent/Null]
set sink(15) [new Agent/Null]

#attaching TCP src to the attacker node
set numSrcs 14
for {set i 1} {$i <= $numSrcs} {incr i} {
    $n(20) attach $src($i) 7771
}

#attaching TCP sink to target nodes
$n(19) attach $sink(1) 21
$n(23) attach $sink(2) 21
$n(21) attach $sink(3) 21
$n(22) attach $sink(4) 21
$n(12) attach $sink(5) 21
$n(14) attach $sink(6) 21
$n(11) attach $sink(7) 21
$n(13) attach $sink(8) 21
$n(15) attach $sink(9) 21
$n(16) attach $sink(10) 21
$n(24) attach $sink(11) 21
$n(25) attach $sink(12) 21
$n(18) attach $sink(13) 21
$n(17) attach $sink(14) 21


#Connect for all probes
set numProbes 14
for {set i 1} {$i <= $numProbes} {incr i} {
    $ns connect $src($i) $sink($i)
}


#Setup FTP applications to simulate traffic over TCP connections
set numFTPAgents 14
for {set i 1} {$i <= $numFTPAgents} {incr i} {
    set ftp($i) [new Application/Traffic/CBR]
    $ftp($i) set packetSize_ 200
    $ftp($i) set interval_ 0.05
    $ftp($i) attach-agent $src($i)
}

proc traffic {app start_time stop_time} {
    global ns
    $ns at $start_time "$app start"
    $ns at $stop_time "$app stop"
}

#Start simulation
set numProbeSes 14
for {set i 1} {$i <= $numProbeSes} {incr i} {
    traffic $ftp($i) 0.1 1.1
}

$ns at 1.2 "finish"

$ns run

